package org.adw.launcher2.ld.secret.ui.utils;

import org.adw.launcher2.ld.secret.db.MyVideoCache;
import org.adw.launcher2.ld.secret.dom.MyPhoto;
import org.adw.launcher2.ld.secret.dom.MyVideo;
import org.adw.launcher2.ld.secret.ui.MyHiddenVideoActivity;
import org.adw.launcher2.ld.secret.ui.MyNonHiddenVideoActivity;


import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

public class VideoImageLoader extends AsyncTask<Void, Bitmap, Bitmap> {
	private ImageView imageView;
	public Bitmap myBitmap;
	private MyVideo myVideo;
	private Context context;
	public VideoImageLoader(Context context, MyVideo myVideo, ImageView imageView){
		this.context = context;
		this.myVideo = myVideo;
		this.imageView = imageView;
	}


	@Override
	protected Bitmap doInBackground(Void... params) {
		this.myBitmap = null;
		if (this.context.toString().toLowerCase().contains(
				"mynonhiddenvideoactivity")) {
			Log.d("ImageLoader", "loadImageFromMemory");
			this.myBitmap = MyVideoCache.getInstance(Global.dbHelper)
					.getImageOfVideoFromMemory(Global.cr,
							this.myVideo.getId() + "");
		}else {
			this.myBitmap = MyVideoCache.getInstance(Global.dbHelper)
					.getImageOfVideosFromDb(this.myVideo.getId() + "");
		}
		return this.myBitmap;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		if (result != null) {
			imageView.setImageBitmap(result);

			if (this.context.toString().toLowerCase().contains(
					"mynonhiddenvideoactivity")) {
				int temp = MyNonHiddenVideoActivity.listAfterHide.indexOf(this.myVideo);
				MyNonHiddenVideoActivity.listAfterHide.get(temp).image = result;
			} else {
				int temp = MyHiddenVideoActivity.listAfterRestore.indexOf(this.myVideo);
				MyHiddenVideoActivity.listAfterRestore.get(temp).image = result;
			}
			this.myVideo.image = result;
		}
	}
}
